<?php

namespace app\canteen\admin;

use app\common\builder\ZBuilder;
use app\common\model\StUser as StUserModel;
class StUser extends Base
{
    public function index()
    {
        $map = $this->getMap();
        if (UID > 1) $map =['comid'=>COMID,'delete_time'=>['>',0]];
        $order = $this->getOrder('create_time desc');
        $list = StUserModel::where($map)->order($order)->paginate();
        $page = $list->render();
        return ZBuilder::make('table')
            ->addColumns([
                ['id', 'ID'],
                ['user_code', '用户编号', 'text'],
                ['user_name', '用户姓名', 'text'],
               // ['comid', '单位', 'text'],
                ['create_time', '创建时间', 'date'],
                ['status', '状态','switch'],
                ['right_button', '操作', 'btn']
            ])
            //批量添加顶部按钮
            ->addTopButtons('add,enable,disable')
            // 批量添加右侧按钮
            ->addRightButtons('edit')
            //设置排序
            ->addOrder('id,create_time')
            // 设置搜索参数
            ->setSearch(['user_name' => '名称','id'=>'ID号'])
            //设置筛选
            ->addFilterList('status', [0=>'禁用', 1=>'启用'])
            ->setTableName('st_user')
            ->setRowList($list)
            ->setPages($page)
            ->fetch();
    }

    //新增
    public function add()
    {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'StUser');
            if(true !== $result) $this->error($result);

            if ($re=StUserModel::create($data)) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','uid',UID],
                ['hidden','comid',COMID],
                ['text', 'user_code', '用户编号'],
                ['text', 'user_name', '用户姓名'],
                ['text', 'user_phone', '用户手机号'],
                ['radio', 'status', '立即启用', '', ['否', '是'], 1]
            ])
            ->layout(['user_num'=>7,'user_name'=>7,'user_phone'=>7])
            ->fetch();
    }

    //编辑
    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'StUser');
            if(true !== $result) $this->error($result);
            if (StUserModel::update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        }

        $info = StUserModel::get($id);
        // 显示编辑页面
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['hidden','comid'],
                ['text', 'user_code', '用户编号'],
                ['text', 'user_name', '用户姓名'],
                ['text', 'user_phone', '用户手机号'],
                ['radio', 'status', '立即启用', '', ['否', '是'], 1]
            ])
            ->setFormdata($info)
            ->layout(['user_num'=>7,'user_name'=>7,'user_phone'=>7])
            ->fetch();
    }
}